//
//  SwipeTableCellViewController.m
//  UILbrary
//
//  Created by mac on 16/12/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "SwipeTableCellViewController.h"
#import "SwipeCell.h"

static NSString *const cellId = @"SwipeCell.h";

@interface SwipeTableCellViewController ()<UITableViewDelegate, UITableViewDataSource,MGSwipeTableCellDelegate>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dateArr;

@end

@implementation SwipeTableCellViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.tableview];
}


#pragma mark - event methods

- (void)deleteMethods{
    NSLog(@"删除");
}

- (void)editoMethods{
    NSLog(@"编辑");
}

#pragma mark -  MGSwipeTableCellDelegate methods
//判断cell是否可以左右滑动
- (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction fromPoint:(CGPoint)point{
    if (direction == MGSwipeDirectionRightToLeft) {
        return YES;
    }
    return NO;
}

//点击按钮cell取消滑动展开状态
-(BOOL) swipeTableCell:(nonnull MGSwipeTableCell*) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion{
    
    if (MGSwipeDirectionRightToLeft) {
        switch (index) {
            case 0:
            {
               [self deleteMethods];
                NSIndexPath *path = [self.tableview indexPathForCell:cell];
                [self.dateArr removeObjectAtIndex:path.row];
                [self.tableview deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationFade];
            }
                break;
            case 1:
            {
                [self editoMethods];
            }
            default:
                break;
        }
    }
    return YES;
}

-(NSArray*) swipeTableCell:(MGSwipeTableCell*) cell swipeButtonsForDirection:(MGSwipeDirection)direction
             swipeSettings:(MGSwipeSettings*) swipeSettings expansionSettings:(MGSwipeExpansionSettings*) expansionSettings;
{
    if (direction == MGSwipeDirectionRightToLeft) {
        expansionSettings.fillOnTrigger = NO;
    }
    return nil;
}
#pragma mark - tabelview delegate tableview datasouce

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dateArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SwipeCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SwipeCell class]) forIndexPath:indexPath];
    cell.rightButtons = @[[MGSwipeButton buttonWithTitle:@"编辑" backgroundColor:[UIColor blueColor]],
                          [MGSwipeButton buttonWithTitle:@"删除" backgroundColor:[UIColor orangeColor]]];
    cell.rightSwipeSettings.transition = MGSwipeTransitionDrag;
    cell.touchOnDismissSwipe = YES;
    cell.delegate = self;
    cell.textLabel.text = self.dateArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        _tableview.backgroundColor = [UIColor whiteColor];
        _tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableview.delegate = self;
        _tableview.dataSource = self;
        [_tableview registerClass:[SwipeCell class] forCellReuseIdentifier:NSStringFromClass([SwipeCell class])];
    }
    return _tableview;
}

- (NSMutableArray *)dateArr{
    if (!_dateArr) {
        _dateArr = [NSMutableArray array];
        for (int cout = 0; cout <20; cout ++) {
            [_dateArr addObject:[NSString stringWithFormat:@"cell 标题 %d",cout]];
        }
    }
    return _dateArr;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
